<template>
  <div class="login-container">
    <!-- 添加背景装饰 -->
    <div class="login-background">
      <div class="shape shape1"></div>
      <div class="shape shape2"></div>
      <div class="shape shape3"></div>
      <div class="particles"></div>
    </div>

    <div class="login-box">
      <!-- 左侧插画区域 -->
      <div class="illustration-box">
        <img src="@/assets/login-illustration.svg" alt="登录插画" />
        <h2>欢迎使用信息系统</h2>
        <p>高效管理，轻松考勤</p>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-form">
        <div class="form-header">
          <h1>用户登录</h1>
          <p>请输入您的账号和密码</p>
        </div>

        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          @keyup.enter.native="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
            >
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            >
            </el-input>
          </el-form-item>

          <div class="login-options">
            <el-checkbox v-model="rememberPassword">记住密码</el-checkbox>
            <el-button type="text" @click="forgotPassword">注册账号</el-button>
          </div>

          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? "登录中..." : "登 录" }}
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginPage",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      loading: false,
      rememberPassword: false,
    };
  },
  created() {
    this.checkSavedCredentials();
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate();
        this.loading = true;

        // 使用 Vuex action 进行登录
        const res = await this.$store.dispatch("user/login", {
          username: this.loginForm.username,
          password: this.loginForm.password,
        });

        if (res.token) {
          // 如果选择记住密码，保存登录信息
          if (this.rememberPassword) {
            this.saveCredentials();
          } else {
            this.clearCredentials();
          }
          // 登录成功后跳转到首页
          this.$router.push("/home");
          this.$message.success("登录成功");
        }
      } catch (error) {
        this.$message.error("登录失败，请检查账号密码");
      } finally {
        this.loading = false;
      }
    },

    // 保存登录凭证
    saveCredentials() {
      localStorage.setItem(
        "rememberedUser",
        JSON.stringify({
          username: this.loginForm.username,
          password: this.loginForm.password,
        })
      );
    },

    // 清除保存的登录凭证
    clearCredentials() {
      localStorage.removeItem("rememberedUser");
    },

    // 检查是否有保存的登录信息
    checkSavedCredentials() {
      const savedUser = localStorage.getItem("rememberedUser");
      if (savedUser) {
        const { username, password } = JSON.parse(savedUser);
        this.loginForm.username = username;
        this.loginForm.password = password;
        this.rememberPassword = true;
      }
    },

    // 忘记密码处理
    forgotPassword() {
      this.$router.push("/test");
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #f0f2f5;
  overflow: hidden;
}

// 添加背景装饰样式
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  overflow: hidden;

  .shape {
    position: absolute;
    border-radius: 50%;
    background: linear-gradient(
      45deg,
      rgba(64, 158, 255, 0.1),
      rgba(24, 144, 255, 0.1)
    );
  }

  .shape1 {
    width: 600px;
    height: 600px;
    top: -200px;
    right: -100px;
  }

  .shape2 {
    width: 400px;
    height: 400px;
    bottom: -150px;
    left: -150px;
  }

  .shape3 {
    width: 300px;
    height: 300px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    opacity: 0.1;
  }

  .particles {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: radial-gradient(
        circle at 25% 25%,
        rgba(64, 158, 255, 0.1) 1px,
        transparent 1px
      ),
      radial-gradient(
        circle at 75% 75%,
        rgba(24, 144, 255, 0.1) 1px,
        transparent 1px
      );
    background-size: 50px 50px;
    animation: particleMove 20s linear infinite;
  }
}

.login-box {
  position: relative;
  z-index: 1;
  width: 900px;
  height: 560px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  display: flex;
  overflow: hidden;
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-5px);
  }
}

.illustration-box {
  flex: 1;
  background: linear-gradient(135deg, #409eff 0%, #1890ff 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  text-align: center;

  img {
    width: 90%;
    max-width: 400px;
    margin-bottom: 30px;
    animation: float 6s ease-in-out infinite;
  }

  h2 {
    font-size: 28px;
    margin-bottom: 15px;
    font-weight: 500;
  }

  p {
    font-size: 16px;
    opacity: 0.9;
  }
}

.login-form {
  width: 400px;
  padding: 40px;
  display: flex;
  flex-direction: column;

  .form-header {
    text-align: center;
    margin-bottom: 40px;

    h1 {
      font-size: 28px;
      color: #303133;
      margin-bottom: 10px;
    }

    p {
      font-size: 14px;
      color: #909399;
    }
  }

  .el-form-item {
    margin-bottom: 25px;

    .el-input {
      .el-input__inner {
        height: 45px;
        line-height: 45px;
        border-radius: 8px;
        padding-left: 45px;
        border: 1px solid #dcdfe6;
        transition: all 0.3s;

        &:focus {
          border-color: #409eff;
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }

        &:hover {
          border-color: #409eff;
        }
      }

      .el-input__prefix {
        left: 15px;
        color: #909399;
      }
    }
  }

  .login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;

    .el-checkbox {
      .el-checkbox__label {
        color: #606266;
      }
    }

    .el-button--text {
      color: #409eff;
      padding: 0;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .login-button {
    height: 45px;
    font-size: 16px;
    border-radius: 8px;
    background: #409eff;
    border: none;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 15px rgba(64, 158, 255, 0.3);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
  100% {
    transform: translateY(0px);
  }
}

// 修改移动端适配样式
@media screen and (max-width: 768px) {
  .login-container {
    padding: 15px;
    min-height: 100vh;
    background: #f0f2f5;
    align-items: flex-start;
  }

  .login-box {
    width: 100%;
    height: auto;
    min-height: auto;
    flex-direction: column;
    margin: 0;
    padding: 0;
    transform: none;

    &:hover {
      transform: none;
    }
  }

  .illustration-box {
    padding: 30px 20px;
    height: auto;
    background: linear-gradient(135deg, #409eff 0%, #1890ff 100%);
    border-radius: 20px 20px 0 0;

    img {
      width: 45%;
      max-width: 160px;
      margin-bottom: 15px;
      animation: none;
    }

    h2 {
      font-size: 20px;
      margin-bottom: 5px;
    }

    p {
      font-size: 14px;
      opacity: 0.8;
      margin: 0;
    }
  }

  .login-form {
    width: 100%;
    padding: 25px 20px;
    background: #fff;
    border-radius: 0 0 20px 20px;

    .form-header {
      margin-bottom: 25px;

      h1 {
        font-size: 22px;
        margin-bottom: 5px;
      }

      p {
        font-size: 13px;
      }
    }

    .el-form-item {
      margin-bottom: 20px;

      .el-input__inner {
        height: 42px;
      }
    }

    .login-options {
      margin-bottom: 20px;
    }

    .login-button {
      width: 100%;
      height: 44px;
      margin-top: 10px;
      font-size: 16px;
      border-radius: 8px;

      &:hover {
        transform: none;
        box-shadow: none;
      }

      &:active {
        opacity: 0.9;
      }
    }
  }
}

// 小屏幕优化 (iPhone SE 等)
@media screen and (max-width: 375px) {
  .login-container {
    padding: 10px;
  }

  .illustration-box {
    padding: 20px 15px;

    img {
      width: 40%;
      max-width: 120px;
      margin-bottom: 10px;
    }

    h2 {
      font-size: 18px;
    }

    p {
      font-size: 12px;
    }
  }

  .login-form {
    padding: 20px 15px;

    .form-header {
      margin-bottom: 20px;
    }

    .el-form-item {
      margin-bottom: 15px;
    }

    .login-button {
      height: 42px;
    }
  }
}

// 横屏优化
@media screen and (max-width: 768px) and (orientation: landscape) {
  .login-container {
    padding: 10px;
    align-items: center;
  }

  .login-box {
    flex-direction: row;
    max-height: 85vh;
  }

  .illustration-box {
    width: 35%;
    padding: 20px 15px;
    border-radius: 20px 0 0 20px;

    img {
      width: 70%;
      max-width: 140px;
      margin-bottom: 10px;
    }
  }

  .login-form {
    width: 65%;
    border-radius: 0 20px 20px 0;
    overflow-y: auto;

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 3px;
    }

    .login-button {
      width: 100%;
      margin-bottom: 10px;
    }
  }
}

// 平板适配
@media screen and (min-width: 769px) and (max-width: 1024px) {
  .login-box {
    width: 90%;
    max-width: 800px;
  }

  .illustration-box {
    img {
      width: 85%;
      max-width: 320px;
    }
  }
}

// 添加动画
@keyframes particleMove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 50px 50px;
  }
}

// 添加悬浮效果
.login-box {
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-5px);
  }
}

// 优化输入框样式
.el-input {
  .el-input__inner {
    &:hover {
      border-color: #409eff;
    }
  }
}

// 优化按钮悬浮效果
.login-button {
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 15px rgba(64, 158, 255, 0.3);
  }
}

// 优化记住密码选项样式
.login-options {
  .el-checkbox {
    .el-checkbox__label {
      color: #606266;
    }
  }

  .el-button--text {
    color: #409eff;

    &:hover {
      color: #1890ff;
    }
  }
}
</style>
